package com.zzh.takeoutapp.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.adapter.TagAdapter3;
import com.zzh.takeoutapp.entity.FoodBean;
import com.zzh.takeoutapp.entity.FoodKey;
import com.zzh.takeoutapp.view.folw.FlowTagLayout;
import com.zzh.takeoutapp.view.folw.OnTagSelectListener;
import com.zzh.takeoutapp.view.goods.AddWidget;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zzh
 * @Name:
 * @Description:
 * @date
 * @time
 * @copyright
 */
public class SpecDialogFragment extends DialogFragment implements View.OnClickListener {
    @Bind(R.id.tv_price)
    TextView tv_price;
    @Bind(R.id.tv_state)
    TextView tv_state;
    @Bind(R.id.specification)
    LinearLayout specification;
    @Bind(R.id.addwidget)
    AddWidget detail_add;
    @Bind(R.id.iv_cancel)
    ImageView iv_cancel;
    private FlowTagLayout mSizeFlowLayout;
    private static SpecDialogFragment instence;
    private TagAdapter3<String> mSizeTagAdapter;
    private BaseQuickAdapter dAdapter;
    public List<FoodKey> foodKeyList = new ArrayList<>();
    public List<String> stringList = new ArrayList<>();
    private boolean iscontainsAll = false;
    private BigDecimal getPrice;
    public FoodBean foodBean;
    private String spec_id_next;
    public List<String> list1 = new ArrayList<>();//选中集合
    public Map<String, String> stringStringMap;

    public void setFoodBean(FoodBean foodBean) {
        this.foodBean = foodBean;
    }

    private AddWidget.OnAddClick onAddClick;

    public void setdAdapter(BaseQuickAdapter dAdapter) {
        this.dAdapter = dAdapter;
    }

    public void setOnAddClick(AddWidget.OnAddClick onAddClick) {
        this.onAddClick = onAddClick;
    }


    public static SpecDialogFragment getInstence() {
        if (instence == null) {
            instence = new SpecDialogFragment();
        }
        return instence;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        dialog.setCancelable(true);
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.95), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View mView = inflater.inflate(R.layout.dialog_serach_name_fragment, null);
        ButterKnife.bind(this, mView);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        final List<FoodBean.SpecsBean> specsX = foodBean.getSpecsX();
        stringStringMap = new HashMap<>();
        if (specsX != null && specsX.size() > 0) {
            getPrice = BigDecimal.valueOf(Double.parseDouble(specsX.get(0).getPriceX())).setScale(1, BigDecimal.ROUND_HALF_DOWN);
        } else {
            getPrice = foodBean.getPrice();
        }
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT
                , LinearLayout.LayoutParams.WRAP_CONTENT);
        specification.setLayoutParams(params);
        specification.removeAllViews();
        final List<FoodBean.SpecificationBean> specification = foodBean.getSpecification();
        /**
         * 清除list
         */
        list1.clear();
        stringList.clear();
        if (specsX != null && specsX.size() > 0) {
            LinearLayout linearLayout = (LinearLayout)
                    View.inflate(getActivity(), R.layout.item_home_order_spce, null);
            final TextView textView = linearLayout.findViewById(R.id.tv_key);
            textView.setText("规格");
            mSizeFlowLayout = linearLayout.findViewById(R.id.size_flow_layout);
            mSizeTagAdapter = new TagAdapter3<>(getActivity());
            mSizeFlowLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
            for (int i = 0; i < specsX.size(); i++) {
                stringList.add(specsX.get(i).getSpec_name());
            }
            /**
             * 第一次添加
             */
            tv_price.setText(specsX.get(0).getPriceX());
            stringStringMap.put("口味", stringList.get(0));
            mSizeTagAdapter.onlyAddAll(stringList);
            spec_id_next = specsX.get(0).getSpec_id();
            list1.add(stringList.get(0));
            mSizeFlowLayout.setAdapter(mSizeTagAdapter);
            mSizeTagAdapter.notifyDataSetChanged();
            mSizeFlowLayout.getChildAt(0).setSelected(true);
            this.specification.addView(linearLayout);
            mSizeFlowLayout.setOnTagSelectListener(new OnTagSelectListener() {
                @Override
                public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                    iscontainsAll = false;
                    for (int i = 0; i < selectedList.size(); i++) {
                        stringStringMap.put("口味", stringList.get(selectedList.get(i)));
                        tv_price.setText(specsX.get(selectedList.get(i)).getPriceX());
                        spec_id_next = specsX.get(selectedList.get(i)).getSpec_id();
                        getPrice = BigDecimal.valueOf(Double.parseDouble(specsX.get(selectedList.get(i)).getPriceX())).setScale(1, BigDecimal.ROUND_HALF_DOWN);
                    }
                    list1.clear();
                    for (Map.Entry<String, String> entry : stringStringMap.entrySet()) {
                        list1.add(entry.getValue());
                    }
                    tv_state.setText(list1 + "");
                    for (int i = 0; i < foodKeyList.size(); i++) {
                        if (foodKeyList.get(i).getKey() != null) {
                            boolean b = list1.containsAll(foodKeyList.get(i).getKey());
                            if (b == true && list1.size() == foodKeyList.get(i).getKey().size()
                                    && foodKeyList.get(i).getGood_id().equals(foodBean.getProduct_id())) {
                                FoodBean foodBeanSpecsX = foodKeyList.get(i).getBean();
                                detail_add.setData(dAdapter, onAddClick, new ArrayList<FoodBean>(), foodBeanSpecsX);
                                detail_add.expendAdd(foodBeanSpecsX.getSelectCount());
                                iscontainsAll = true;
                                return;
                            }
                        }
                    }
                    if (iscontainsAll == false) {
                        FoodKey foodKeySpecname = new FoodKey();
                        List<String> getStringList = new ArrayList<>();
                        getStringList.clear();
                        FoodBean foodBeanSpecsX = new FoodBean();
                        foodBeanSpecsX.setSpec_id_next(spec_id_next);
                        foodBeanSpecsX.setSpecsX(foodBean.getSpecsX());
                        foodBeanSpecsX.setSpecification(foodBean.getSpecification());
                        getStringList.addAll(list1);
                        foodBeanSpecsX.setProduct_id(foodBean.getProduct_id() + foodBean.getSpecsX().get(0).getSpec_id() + list1);
                        foodBeanSpecsX.setPrice(getPrice);
                        foodKeySpecname.setBean(foodBeanSpecsX);
                        foodKeySpecname.setGood_id(foodBean.getProduct_id());
                        foodKeySpecname.setKey(getStringList);
                        foodBeanSpecsX.setName(foodBean.getName() + getStringList);
                        foodKeyList.add(foodKeySpecname);
                        detail_add.setData(dAdapter, onAddClick, new ArrayList<FoodBean>(), foodBeanSpecsX);
                        detail_add.expendAdd(foodBeanSpecsX.getSelectCount());
                    }
                }
            });
        }
        if (specification.size() > 0) {
            for (int i = 0; i < specification.size(); i++) {
                final LinearLayout linearLayout = (LinearLayout) View.inflate(getActivity(),
                        R.layout.item_home_order_spce, null);
                TextView textView = linearLayout.findViewById(R.id.tv_key);
                final String key = specification.get(i).getKey();
                textView.setText(specification.get(i).getKey());
                final List<String> val = specification.get(i).getVal();
                mSizeTagAdapter = new TagAdapter3<>(getActivity());
                mSizeFlowLayout = linearLayout.findViewById(R.id.size_flow_layout);
                mSizeTagAdapter.onlyAddAll(val);
                mSizeFlowLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
                mSizeFlowLayout.setAdapter(mSizeTagAdapter);
                mSizeTagAdapter.notifyDataSetChanged();
                mSizeFlowLayout.getChildAt(0).setSelected(true);
                /**
                 * 添加选中的
                 */
                stringStringMap.put(key, val.get(0));
                list1.add(val.get(0));
                mSizeFlowLayout.setOnTagSelectListener(new OnTagSelectListener() {
                    @Override
                    public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                        iscontainsAll = false;
                        for (int i = 0; i < selectedList.size(); i++) {
                            stringStringMap.put(key, val.get(selectedList.get(i)));
                        }
                        list1.clear();
                        for (Map.Entry<String, String> entry : stringStringMap.entrySet()) {
                            list1.add(entry.getValue());
                        }
                        tv_state.setText(list1 + "");
                        for (int i = 0; i < foodKeyList.size(); i++) {
                            if (foodKeyList.get(i).getKey() != null) {
                                boolean b = list1.containsAll(foodKeyList.get(i).getKey());
                                if (b == true && list1.size() == foodKeyList.get(i).getKey().size()
                                        && foodKeyList.get(i).getGood_id().equals(foodBean.getProduct_id())) {
                                    FoodBean fBeanKey = foodKeyList.get(i).getBean();
                                    detail_add.setData(dAdapter, onAddClick, new ArrayList<FoodBean>(), fBeanKey);
                                    detail_add.expendAdd(fBeanKey.getSelectCount());
                                    iscontainsAll = true;
                                    return;
                                }
                            }
                        }
                        if (iscontainsAll == false) {
                            FoodKey foodKeySpecname = new FoodKey();
                            List<String> getStringList = new ArrayList<>();
                            getStringList.clear();
                            FoodBean fBeanKey = new FoodBean();
                            fBeanKey.setPrice(getPrice);
                            fBeanKey.setStringStringMap(stringStringMap);
                            fBeanKey.setName(foodBean.getName() + list1);
                            getStringList.addAll(list1);
                            foodKeySpecname.setGood_id(foodBean.getProduct_id());
                            if (foodBean.getSpecsX() != null && foodBean.getSpecsX().size() > 0) {
                                fBeanKey.setSpecsX(foodBean.getSpecsX());
                                fBeanKey.setProduct_id(foodBean.getProduct_id() + foodBean.getSpecsX().get(0).getSpec_id() + getStringList);
                                fBeanKey.setSpecification(foodBean.getSpecification());
                            } else {
                                fBeanKey.setSpecsX(foodBean.getSpecsX());
                                fBeanKey.setSpecification(foodBean.getSpecification());
                                fBeanKey.setProduct_id(foodBean.getProduct_id() + foodBean.getSpecification().get(0).getKey() + getStringList);
                            }
                            foodKeySpecname.setBean(fBeanKey);
                            foodKeySpecname.setKey(getStringList);
                            foodKeyList.add(foodKeySpecname);
                            detail_add.setData(dAdapter, onAddClick, new ArrayList<FoodBean>(), fBeanKey);
                            detail_add.expendAdd(fBeanKey.getSelectCount());
                        }
                    }
                });
                this.specification.addView(linearLayout);
            }
        }
        tv_state.setText(list1 + "");
        if (foodKeyList.size() == 0) {
            FoodBean fBean = new FoodBean();
            getFoodBean(fBean);
            detail_add.setState(fBean.getSelectCount());
        } else {
            for (int i = 0; i < foodKeyList.size(); i++) {
                boolean b = list1.containsAll(foodKeyList.get(i).getKey());
                if (b == true && list1.size() == foodKeyList.get(i).getKey().size()
                        && foodKeyList.get(i).getGood_id().equals(foodBean.getProduct_id())) {
                    FoodBean fBeanKey = foodKeyList.get(i).getBean();
                    detail_add.setData(dAdapter, onAddClick, new ArrayList<FoodBean>(), fBeanKey);
                    detail_add.expendAdd(fBeanKey.getSelectCount());
                    break;
                } else if (b == false) {
                    FoodBean fBean = new FoodBean();
                    getFoodBean(fBean);
                    detail_add.expendAdd(fBean.getSelectCount());
                }
            }
        }
        return mView;
    }

    private void getFoodBean(FoodBean fBean) {
        fBean.setPrice(getPrice);
        fBean.setSpec_id_next(spec_id_next);
        fBean.setStringStringMap(stringStringMap);
        fBean.setSpecsX(foodBean.getSpecsX());
        fBean.setSpecification(foodBean.getSpecification());
        if (foodBean.getSpecsX() != null && foodBean.getSpecsX().size() > 0) {
            fBean.setProduct_id(foodBean.getProduct_id() + foodBean.getSpecsX().get(0).getSpec_id() + list1);
        } else {
            fBean.setProduct_id(foodBean.getProduct_id() + foodBean.getSpecification().get(0).getKey() + list1);
        }
        FoodKey foodKeySpecname = new FoodKey();
        List<String> getStringList = new ArrayList<>();
        getStringList.clear();
        getStringList.addAll(list1);
        fBean.setName(foodBean.getName() + getStringList);
        foodKeySpecname.setBean(fBean);
        foodKeySpecname.setGood_id(foodBean.getProduct_id());
        foodKeySpecname.setKey(getStringList);
        foodKeyList.add(foodKeySpecname);
        detail_add.setData(dAdapter, onAddClick, new ArrayList<FoodBean>(), fBean);
    }

    public static List<FoodKey> removeDuplicate(List<FoodKey> list) {
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
        return list;
    }

    @OnClick({R.id.iv_cancel})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cancel:
                dismiss();
                break;
        }
    }


}
